package com.dji.djiapp.activity;

import static android.view.View.VISIBLE;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import com.dji.djiapp.MApplication;
import com.dji.djiapp.R;
import com.dji.djiapp.config.ModuleVerificationUtil;
import com.dji.djiapp.widget.VideoFeedView;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import dji.common.error.DJIError;
import dji.common.gimbal.Rotation;
import dji.common.gimbal.RotationMode;
import dji.common.util.CommonCallbacks;
import dji.sdk.camera.VideoFeeder;
import dji.ux.widget.FPVWidget;

public class TripodControlActivity extends AppCompatActivity {

    @BindView(R.id.zoom_in)
    Button zoomInBtn;
    @BindView(R.id.zoom_out)
    Button zoomOutBtn;
    @BindView(R.id.gimbal_up)
    Button controlUpBtn;
    @BindView(R.id.gimbal_down)
    Button controlDownBtn;
    @BindView(R.id.gimbal_left)
    Button controlLeftBtn;
    @BindView(R.id.gimbal_right)
    Button controlRightBtn;

    private Timer timer;
    private GimbalRotateTimerTask gimbalRotationTimerTask;
    private VideoFeedView fpvVideoFeedView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tripod_control);
        init();
    }

    private void init() {
        ButterKnife.bind(this);

        fpvVideoFeedView = (VideoFeedView) findViewById(R.id.gimbal_view);
        fpvVideoFeedView.registerLiveVideo(VideoFeeder.getInstance().getPrimaryVideoFeed(), true);
        if (ModuleVerificationUtil.isMultiStreamPlatform()){
            fpvVideoFeedView.setVisibility(VISIBLE);
        }
        initOnClickListener();
    }

    private void stop(){
        if (timer != null) {
            if (gimbalRotationTimerTask != null) {
                gimbalRotationTimerTask.cancel();
            }
            timer.cancel();
            timer.purge();
            gimbalRotationTimerTask = null;
            timer = null;
        }

        if (ModuleVerificationUtil.isGimbalModuleAvailable()) {
            MApplication.getProductInstance().getGimbal().
                    rotate(null, new CommonCallbacks.CompletionCallback() {

                        @Override
                        public void onResult(DJIError error) {

                        }
                    });
        }
    }

    private void initOnClickListener() {
        controlDownBtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (timer == null) {
                        timer = new Timer();
                        gimbalRotationTimerTask = new GimbalRotateTimerTask(-10, Rotation.NO_ROTATION);
                        timer.schedule(gimbalRotationTimerTask, 0, 100);
                    }
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                   stop();
                }
                return false;
            }
        });

        controlUpBtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (timer == null) {
                        timer = new Timer();
                        gimbalRotationTimerTask = new GimbalRotateTimerTask(10, Rotation.NO_ROTATION);
                        timer.schedule(gimbalRotationTimerTask, 0, 100);
                    }
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    stop();
                }
                return false;
            }
        });

        controlLeftBtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (timer == null) {
                        timer = new Timer();
                        gimbalRotationTimerTask = new GimbalRotateTimerTask( Rotation.NO_ROTATION, -10);
                        timer.schedule(gimbalRotationTimerTask, 0, 100);
                    }
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    stop();
                }
                return false;
            }
        });

        controlRightBtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (timer == null) {
                        timer = new Timer();
                        gimbalRotationTimerTask = new GimbalRotateTimerTask( Rotation.NO_ROTATION, 10);
                        timer.schedule(gimbalRotationTimerTask, 0, 100);
                    }
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    stop();
                }
                return false;
            }
        });
    }

    private static class GimbalRotateTimerTask extends TimerTask {
        float pitchValue;
        float yawValue;

        GimbalRotateTimerTask(float pitchValue, float yawValue) {
            super();
            this.pitchValue = pitchValue;
            this.yawValue = yawValue;
        }

        @Override
        public void run() {
            if (ModuleVerificationUtil.isGimbalModuleAvailable()) {
                MApplication.getProductInstance().getGimbal().
                        rotate(new Rotation.Builder().pitch(pitchValue)
                                .mode(RotationMode.SPEED)
                                .yaw(yawValue)
                                .roll(Rotation.NO_ROTATION)
                                .time(0)
                                .build(), new CommonCallbacks.CompletionCallback() {

                            @Override
                            public void onResult(DJIError error) {

                            }
                        });
            }
        }
    }
}